package Servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import javabean.Detail;
import javabean.ResultData;
import javabean.User;
import service.UserModuleService;
import serviceimpl.UserModuleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static java.lang.Integer.parseInt;

@WebServlet(name = "UserMainServlet", value = "/userMain")
public class UserMainServlet extends HttpServlet {
    private static final UserModuleService userModuleService = new UserModuleServiceImpl();

    public static ArrayList<Detail> allData = null;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer code = Integer.valueOf(req.getParameter("code"));

        System.out.println(code);

        HttpSession session = req.getSession();
        User u = (User) session.getAttribute("user");

        ObjectMapper js = new ObjectMapper();
        Map<String, Object> map = new HashMap<>();

        switch (code) {
            case -1: {
                session.invalidate();
                map.put("res", -1);
                map.put("redirectUrl", "login.html");
                break;
            }
            case 1: {
                map.put("data", u);
                break;
            }
            case 21:{}
            case 22: {
                int pageSize = parseInt(req.getParameter("pageSize"));
                int currentPage = parseInt(req.getParameter("currentPage"));

                //System.out.println("currentPage:" + currentPage);
                //System.out.println("pageSize:" + pageSize);
                //System.out.println("---------------------------");

                if (allData == null) {
                    allData = userModuleService.findMyDetail(u.getUserName());
                }
                ArrayList<Detail> detailss = new ArrayList<>();
                for (int i = pageSize * (currentPage - 1); i < allData.size() && i < pageSize * currentPage; i++) {
                    detailss.add(allData.get(i));
                }
                ResultData<Detail> resultData = new ResultData<>(allData.size(), "", detailss);

                map.put("resultData", resultData);
                break;
            }
            case 31: {
                String toUser = req.getParameter("toUser");
                String password = req.getParameter("password");
                double amount = Double.parseDouble(req.getParameter("amount"));

                Integer addRes = null;
                String msg = null;

                if(u.getPassWord().equals(password)) {
                    addRes = userModuleService.transfer(u.getUserName(), toUser, amount);
                    if(addRes == 1) {
                        msg = "转账成功";
                        u = userModuleService.findMySelf(u.getId());
                        session.setAttribute("user", u);
                    } else if(addRes == 0) {
                        msg = "转账出错了，请稍后再试";
                    } else {
                        msg = "转账失败，余额不足或者目标账户不对";
                    }
                } else {
                    addRes = -1;
                    msg = "转账失败，密码输入错误";
                }
                map.put("addRes", addRes);
                map.put("msg", msg);
                break;
            }
            case 41: {
                String password = req.getParameter("password");
                String newPassword = req.getParameter("newPassword");
                String reNewPassword = req.getParameter("reNewPassword");
                //System.out.println(password);
                //System.out.println(newPassword);
                //System.out.println(reNewPassword);
                //System.out.println("----------------------");

                Boolean resReset = null;
                String msg = null;
                if(u.getPassWord().equals(password)) {
                    if(newPassword != null && !newPassword.equals("")) {
                        if(newPassword.equals(reNewPassword)) {
                            resReset = userModuleService.userResetPassword(u.getId(), newPassword);
                            msg = "修改密码成功";
                            u = userModuleService.findMySelf(u.getId());
                            session.setAttribute("user", u);
                        } else {
                            resReset = false;
                            msg = "修改失败，两次密码不相同";
                        }
                    } else {
                        resReset = false;
                        msg = "修改失败，新密码为空";
                    }
                } else {
                    resReset = false;
                    msg = "修改失败，旧密码不正确";
                }
                map.put("msg", msg);
                map.put("resReset", resReset);

                break;
            }
            default: {
                map.put("res", "提交状态码错误");
            }
        }
        resp.getWriter().println(js.writeValueAsString(map));
    }
}
